﻿using System;
using System.Web.Mvc;
using Fuse8.ContentManagementFramework.SystemFacade;
using Fuse8.ContentManagementFramework.Web.Etc;
using Fuse8.DomainFramework.Common;

using Fuse8.ContentManagementFramework.DataAccess;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Web.ApplicationRuntime;
using Fuse8.ContentManagementFramework.BizFacade;
using Fuse8.ContentManagementFramework.SystemFacade.Common;
using Fuse8.DomainFramework.Common.Locator;

namespace Fuse8.ContentManagementFramework.Web.StartUp
{
    internal static class WebPolicy
    {
        public static void Init(Func<object, string> dataConnectionResolver)
        {
            RegisterDataAccess(dataConnectionResolver);

            RegisterBizServices();

            CmsCacheDependency.Init();

            RegisterWebFeatures();
        }

        private static void RegisterDataAccess(Func<object, string> dataConnectionResolver)
        {
            DataAccessPolicy.Register(dataConnectionResolver);
        }

        private static void RegisterBizServices()
        {
            BusinessFacadePolicy.Register();
        }

        private static void RegisterWebFeatures()
        {
            var context = new DefaultCmsContext();
            DependencyServiceLocator.Current.Register<ICmsContext>(context);
            DependencyServiceLocator.Current.Register<IMapPath>(context);
            DependencyServiceLocator.Current.Register<ICmsPageUrlResolver>(new DefaultCmsPageUrlResolver());
            DependencyServiceLocator.Current.SetDefaultService<ITemplateResolver>(new NustacheTemplateResolver());

            ControllerBuilder.Current.SetControllerFactory(new CmsControllerFactory());

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.AddCustom<RazorViewEngine>(p => p.IsInAdministrationArea(), "admin");
        }
    }
}
